﻿using AdverseEventReportSystem.BLL.ServiceReference1;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdverseEventReportSystem.BLL
{
    public class DepartmentManager : BaseManager
    {
        /// <summary>
        /// 获取所有病区
        /// </summary>
        /// <returns></returns>
        public static OperateResult GetAllUnits()
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfDataTable result = service.getAllUnits();
                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 获取病区信息
        /// </summary>
        /// <param name="DepartmentCode"></param>
        /// <returns></returns>
        public static OperateResult GetDetaildep(string departmentCode)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOftb_Department result = service.Detaildep(departmentCode);
                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>
        public static OperateResult Save(tb_Department department)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                Loginuser loginUser = LoginManager.GetLoginUser();
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();

                if (department.Id == 0)
                {
                    if (string.IsNullOrEmpty(department.ParentId.ToString()))
                    {
                        department.ParentId = 0;
                    }
                    ResultHelperOftb_Department result = service.Adddep(department);
                    if (!result.S)
                    {
                        resultInfo.Result = 0;
                        resultInfo.Body = result.M;
                    }
                }
                else
                {
                    ResultHelperOfInt32 result = service.udpatedep(department);
                    if (!result.S)
                    {
                        resultInfo.Result = 0;
                        resultInfo.Body = result.M;
                    }
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static OperateResult Delete(int id)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfInt32 result = service.Deldep(id.ToString());
                if (!result.S)
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }
    }
}
